package com.hxkcloud.nurse.dborm;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.table.TableUtils;
import com.hxkcloud.nurse.YjhApplication;
import com.hxkcloud.nurse.beans.ArticleKindBean;
import com.hxkcloud.nurse.beans.ArticleKindChildBean;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by william_sun on 2016-07-22.
 * Description:
 */
public class ArticleKindDao {

    private Context context;
    private Dao<ArticleKindBean, Integer> articleKindDao;
    private ArticleKindChildDao childDao;

    public ArticleKindDao(Context context) {
        this.context = context;
        try {
            articleKindDao = DatabaseHelper.getHelper(context).getDao(ArticleKindBean.class);
            childDao = new ArticleKindChildDao(context);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<ArticleKindBean> getKinds(String departmentId, String type) {
        try {
            QueryBuilder builder = articleKindDao.queryBuilder();
            builder.where().eq("departmentId", departmentId);
            builder.where().eq("type", type);
            List<ArticleKindBean> kinds = builder.query();
            for (ArticleKindBean kind : kinds) {
                kind.setChildList(childDao.getChild(kind.getItemId()));
            }
            return kinds;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int getKindsNum() {
        try {
            if (articleKindDao.queryForAll() == null) {
                return 0;
            } else {
                return articleKindDao.queryForAll().size();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public void setKind(List<ArticleKindBean> kinds_news, List<ArticleKindBean> kinds_course, String departmentId) {
        try {
            clearKind();
            for (ArticleKindBean kind : kinds_news) {
                kind.setDepartmentId(departmentId);
                kind.setType(YjhApplication.articlekind_news);
                articleKindDao.create(kind);
                for (ArticleKindChildBean child : kind.getChildList()) {
                    child.setItemPId(kind.getItemId());
                    childDao.setChild(child);
                }

            }
            for (ArticleKindBean kind : kinds_course) {
                kind.setDepartmentId(departmentId);
                kind.setType(YjhApplication.articlekind_course);
                articleKindDao.create(kind);
                for (ArticleKindChildBean child : kind.getChildList()) {
                    child.setItemPId(kind.getItemId());
                    childDao.setChild(child);
                }

            }
        } catch (SQLException e) {
        }
    }

    public void clearKind() {
        try {
            TableUtils.clearTable(articleKindDao.getConnectionSource(), ArticleKindBean.class);
            childDao.clearChild();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public class ArticleKindChildDao {
        private Context context;
        private Dao<ArticleKindChildBean, Integer> articleKindChildDao;

        public ArticleKindChildDao(Context context) {
            this.context = context;
            try {
                articleKindChildDao = DatabaseHelper.getHelper(context).getDao(ArticleKindChildBean.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        public List<ArticleKindChildBean> getChild(String kId) {
            try {
                QueryBuilder builder = articleKindChildDao.queryBuilder();
                builder.where().eq("itemPID", kId);
                return builder.query();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
        }

        public void setChild(ArticleKindChildBean test) {
            try {
                articleKindChildDao.create(test);
            } catch (SQLException e) {
            }
        }

        public void setChild(List<ArticleKindChildBean> childs) {
            try {
                for (ArticleKindChildBean c : childs) {
                    articleKindChildDao.create(c);
                }
            } catch (SQLException e) {
            }
        }

        public void clearChild() {
            try {
                TableUtils.clearTable(articleKindChildDao.getConnectionSource(), ArticleKindChildBean.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
